package shootingtactics.client.core.enemy;

import java.util.ArrayList;

import shootingtactics.client.core.Constant;
import shootingtactics.client.core.DrawingCore;
import shootingtactics.client.core.bullet.BaseBullet;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;

public class BaseEnemy extends DrawingCore{

    BaseEnemy targetEnemy;

    int baseSpeed;

    int speedX;

    int speedY;

    public int health;

    ArrayList<BaseBullet> bulletList = new ArrayList<BaseBullet>();

    ArrayList<BaseBullet> deleteBulletList;

    long bulletTime = System.currentTimeMillis();

    public BaseEnemy(double posX, double posY, String drawType, CssColor drawColor, Context2d targetContext){
        super(posX, posY, drawType, drawColor, targetContext);
    }

    public double getTargetEnemyPositionX(){
        return targetEnemy.position.getPositionX();
    }

    public double getTargetEnemyPositionY(){
        return targetEnemy.position.getPositionY();
    }

    public void setTargetEnemy(BaseEnemy targetEnemy){
        this.targetEnemy = targetEnemy;
    }

    public void updateSpeed(){
        long time = System.currentTimeMillis();
        updateBullet();
        position.setPosition(position.getPositionX() + speedX, position.getPositionY() + speedY);
        System.out.println("BulletDrawTime:" + (System.currentTimeMillis() - time));
    }

    public void updateBullet(){

        deleteBulletList = new ArrayList<BaseBullet>();
        int size = bulletList.size();
        System.out.println("BulletSize:" + size);
        for(int i = 0;i < size;i++){
            BaseBullet bullet = bulletList.get(i);
            double posX = bullet.position.getPositionX();
            double posY = bullet.position.getPositionY();
            double targetPosX = bullet.targetEnemy.position.getPositionX();
            double targetPosY = bullet.targetEnemy.position.getPositionY();
            double width = bullet.targetEnemy.position.getWidth();
            double height = bullet.targetEnemy.position.getHeight();
            if(posX < 0 - Constant.CANVAS_LIMIT_WIDTH || posX > Constant.CANVAS_WIDTH + Constant.CANVAS_LIMIT_WIDTH
                || posY < 0 - Constant.CANVAS_LIMIT_HEIGHT || posY > Constant.CANVAS_HEIGHT + Constant.CANVAS_LIMIT_HEIGHT){
                deleteBulletList.add(bullet);
            }else if(posX > targetPosX - width / 2 && posX < targetPosX + width / 2 && posY > targetPosY - height / 2 && posY < targetPosY + height / 2){
                bullet.targetEnemy.health -= 1;
                deleteBulletList.add(bullet);
            }else{
                bullet.updateSpeed();
                bullet.setNextPosition();
                bullet.setDraw();
            }
        }
        bulletList.removeAll(deleteBulletList);
    }
}
